package com.ipartek.formacion.ddbb;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

//import org.apache.log4j.Logger;
//import org.apache.log4j.PropertyConfigurator;


import com.ipartek.desarrollo.dao.AccessMatriculasDAO;
import com.ipartek.desarrollo.interfaces.IMatriculasDAO;

/**
 * 
 * @author Fila3+1
 */
public class AccessDAOFactory extends DAOFactory {
	// inicializo un DataSource (para utilizar el context.xml
	// Parametros configuracion base de datos
	private static final String DRIVER_ACCESS = "sun.jdbc.odbc.JdbcOdbcDriver";
	private static final String DB_NAME = "Formacion";
	private static final String DB_USER = "";
	private static final String DB_PASS = "";
	private static final String DB_URL = "jdbc:odbc:"+DB_NAME;

//	private static final Logger log = Logger.getLogger(AccessDAOFactory.class);

	// Constructor
	// ************************************************************************
	public AccessDAOFactory() {
//		PropertyConfigurator.configure("config/log4j.properties");
	}

	// Metodos
	// ************************************************************************

	// Conectar con la base de datos
	public static Connection conectar() throws Exception {
		Connection conexion = null; // Creamos un tipo connection
		try {
			Class.forName(DRIVER_ACCESS);

//			log.trace("Conectamos con la ddbb");

			conexion = DriverManager.getConnection(DB_URL, DB_USER, DB_PASS);
			conexion.setAutoCommit(true);

//			log.trace("Conectados con la ddbb " + DB_URL);
		} catch (ClassNotFoundException e) {
//			log.error("ClassNotFoundException driver no encontrado " + DRIVER_MYSQL);
		} catch (SQLException e) {
			while (e != null) {
//				log.error("Message: " + e.getMessage() + "SQLState: "+ e.getSQLState() + "ErrorCode: " + e.getErrorCode());
				e = e.getNextException();
			}
		} catch (Exception e) {
//			log.error("Exception general al conectar con la ddbb "+ e.getMessage());
		}
//		log.trace("Devolvemos la conexion con la ddbb");
		return conexion;
	}

	// Desconectar con la base de datos
	public static void desconectar(Connection conexion) {
		if (conexion != null) {
			try {
				conexion.close();
			} catch (SQLException ex) {
//				log.error("Exception general al desconectar con la ddbb "+ ex.getMessage());
			}
		}
	}

	// Metodos que tengo que implementar al extender de DAOFActory
	// *************************************************************************
	@Override
	public IMatriculasDAO getInterfaceMatriculasDAO() {
		// TODO Auto-generated method stub
		return new AccessMatriculasDAO();
	}

}
